#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include "global.h"
unsigned char old_game_status = 0; // 记录旧有的
unsigned char game_status = 1; // 初始状态 游戏开始界面


BlockInfo last_block; // 上一个块
BlockInfo cur_block; // 当前块
BlockInfo next_block; // 下一个块

unsigned char changedir = 0; // 1 变换方向



static int GetNewBlock(BlockInfo * block)
{
    // // 获取当前时间戳
    // time_t timestamp = time(NULL);

    // // 获取当前进程ID和线程ID
    // pid_t processID = getpid();
    // pthread_t threadID = pthread_self();
    // // 构造种子
    // unsigned int seed = (unsigned int)timestamp ^
    //                     (unsigned int)processID ^
    //                     (unsigned int)threadID;
    
    block->block_coord.r = -1;
    block->block_coord.c = 3; // 坐标
    block->block_color = (31 + rand() % 7); // 颜色
    block->block_type = (rand() % 7); // 方块类型
    block->block_form = 0; // 方块形态，默认第一个形态
    memcpy(block->block_array,blocks[block->block_type*4+block->block_form],16); // 拷贝到方块数组里
    
}

// 游戏资源初始化
void GameInit(void)
{
    srand(time(NULL));
    GetNewBlock(&cur_block); // 当前
    DrawBlocks(3,3,cur_block.block_coord.r,cur_block.block_coord.c,cur_block.block_array,cur_block.block_color); // 绘制当前
    GetNewBlock(&next_block); // 下一个方块
    DrawBlocks(17,13,next_block.block_coord.r,next_block.block_coord.c,next_block.block_array,next_block.block_color); // 绘制下一个
}

// 游戏开始界面
void GameStart(void)
{

}

unsigned char JudgeUp()
{
    unsigned char cnt = 0;
    // 从上往下
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            if(cur_block.block_array[i][j])
            {
                // 判断碰到地图边界
                if(cur_block.block_coord.r+i == 0)
                    return 1;
                cnt++;
                if(cnt == 4)
                {
                    return 0;
                }
            }
        }
    } 
    return 0;
}


unsigned char JudgeDown()
{
    unsigned char cnt = 0;
    // 从下往上
    for(int i=3;i>=0;i--)
    {
        for(int j=0;j<4;j++)
        {
            if(cur_block.block_array[i][j])
            {
                // 判断碰到地图边界
                if(cur_block.block_coord.r+i+1 == 20)
                    return 1;
                // 判断碰到地图里面
                if(map[cur_block.block_coord.r+i+1][cur_block.block_coord.c+j] == 1)
                    return 1;
                cnt++;
                if(cnt == 4)
                {
                    return 0;
                }
            }
        }
    } 
    return 0;
}


unsigned char JudgeLeft()
{
    unsigned char cnt = 0;
    // 从左往右
    for(int j=0;j<4;j++)
    {
        for(int i=0;i<4;i++)
        {
            if(cur_block.block_array[i][j])
            {
                // 判断碰到地图边界
                if(cur_block.block_coord.c+j == 0)
                    return 1;
                // 判断碰到地图里面
                if(map[cur_block.block_coord.r+i][cur_block.block_coord.c+j-1] == 1)
                    return 1;

                cnt++;
                if(cnt == 4)
                {
                    return 0;
                }
            }
        }
    } 
    return 0;
}

unsigned char JudgeRight()
{
    unsigned char cnt = 0;
    // 从右往左
    for(int j=3;j>=0;j--)
    {
        for(int i=0;i<4;i++)
        {
            if(cur_block.block_array[i][j])
            {
                // 判断碰到地图边界
                if(cur_block.block_coord.c+j+1 == 10)
                    return 1;
                // 判断碰到地图里面
                if(map[cur_block.block_coord.r+i][cur_block.block_coord.c+j+1] == 1)
                    return 1;

                cnt++;
                if(cnt == 4)
                {
                    return 0;
                }
            }
        }
    } 
    return 0;
}

static int ChangeBlock(BlockInfo * block)
{

    block->block_form++;
    if(block->block_form == 4)
    {
        block->block_form=0;
    }
    memcpy(block->block_array,blocks[block->block_type*4+block->block_form],16); // 拷贝到方块数组里
    
}

// 游戏中
void GameIng(void)
{
    static unsigned int timer_cnt = 0;
    timer_cnt++;
    if (game_status != old_game_status) // 第一次进入
    {
        GameInit();
        DrawGameMapInit(); // 绘制地图静态画面
        
        old_game_status = game_status;
    }


    
    // 空格 直接到底
    if(changedir == 5)
    {
        changedir = 0;
        DrawClearBlocks(3,3,cur_block.block_coord.r,cur_block.block_coord.c,cur_block.block_array,cur_block.block_color);
        while(JudgeDown() == 0)
        {
            cur_block.block_coord.r +=1;
        }
        DrawBlocks(3,3,cur_block.block_coord.r,cur_block.block_coord.c,cur_block.block_array,cur_block.block_color); // 绘制当前
    }
    

    // 向下 速降
    if(changedir == 4)
    {
        changedir = 0;
        if(JudgeDown() == 0) // 判断碰到地图
        {
            DrawClearBlocks(3,3,cur_block.block_coord.r,cur_block.block_coord.c,cur_block.block_array,cur_block.block_color);
            cur_block.block_coord.r +=1;
            DrawBlocks(3,3,cur_block.block_coord.r,cur_block.block_coord.c,cur_block.block_array,cur_block.block_color); // 绘制当前
        }
    }

    // 向左向右
    if(changedir == 2|| changedir == 3)
    {
        if(changedir == 2) // 向左
        {
            changedir = 0;
            if(JudgeLeft() == 0) // 判断碰到地图
            {
                DrawClearBlocks(3,3,cur_block.block_coord.r,cur_block.block_coord.c,cur_block.block_array,cur_block.block_color);
                cur_block.block_coord.c -=1;
                DrawBlocks(3,3,cur_block.block_coord.r,cur_block.block_coord.c,cur_block.block_array,cur_block.block_color); // 绘制当前
            }
            
        }
        else if(changedir == 3) // 向右
        {
            changedir = 0;
            if(JudgeRight() == 0) 
            {
                DrawClearBlocks(3,3,cur_block.block_coord.r,cur_block.block_coord.c,cur_block.block_array,cur_block.block_color);
                cur_block.block_coord.c +=1;
                DrawBlocks(3,3,cur_block.block_coord.r,cur_block.block_coord.c,cur_block.block_array,cur_block.block_color); // 绘制当前
            }
        }
    }
    // 变换方向
    if(changedir == 1)
    {
        DrawClearBlocks(3,3,cur_block.block_coord.r,cur_block.block_coord.c,cur_block.block_array,cur_block.block_color);
        ChangeBlock(&cur_block);
        DrawBlocks(3,3,cur_block.block_coord.r,cur_block.block_coord.c,cur_block.block_array,cur_block.block_color); // 绘制当前
        changedir=0;
    }
    // 判断是否碰撞到地图
    // 判断下方是否触碰到边界 或者 地图里的值
    if(JudgeDown())
    {
        // 判断死亡
        if(JudgeUp())
        {
            DrawClear();
            printf("game_over");
            game_status = 2;
            
        }
        // 清除之前的方块
        last_block = cur_block;
        DrawClearBlocks(3,3,last_block.block_coord.r,last_block.block_coord.c,last_block.block_array,last_block.block_color);

        // 全部拷贝到地图里
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                if(cur_block.block_array[i][j])
                {
                    map[cur_block.block_coord.r+i][cur_block.block_coord.c+j] = 1;
                    mapcolor[cur_block.block_coord.r+i][cur_block.block_coord.c+j] = cur_block.block_color;
                }
            }
        }

        DrawMap(); // 绘制地图
        // 获取新的方块
        cur_block = next_block;
        GetNewBlock(&next_block);
        
        DrawBlocks(3,3,cur_block.block_coord.r,cur_block.block_coord.c,cur_block.block_array,cur_block.block_color); // 绘制当前
        DrawClearBlocks(17,13,cur_block.block_coord.r,cur_block.block_coord.c,cur_block.block_array,cur_block.block_color);
        DrawBlocks(17,13,next_block.block_coord.r,next_block.block_coord.c,next_block.block_array,next_block.block_color); // 绘制下一个
    
        unsigned char cnt = 0;
        // 判断消除
        for(int i=19;i>=0;i--) // 从下往上
        {
            cnt = 0;
            for(int j=0;j<10;j++)
            {
                if(map[i][j]==1)
                {
                    cnt++;
                }
            }
            if(cnt == 10)
            {
                DrawClearMapLine(i);
                // 一行全部清0
                for(int j=0;j<10;j++)
                {
                    map[i][j]=0;
                    mapcolor[i][j]=0;
                } 
                // DrawClearMapLine(i+1);
                // 上面往下拷贝
                for(int x=i;x>=0;x--)
                {
                    for(int y=0;y<10;y++)
                    {
                        if(x==0)
                        {
                            map[x][y] = 0;
                            mapcolor[x][y]=0;
                        }
                        else
                        {
                            map[x][y] = map[x-1][y];
                            mapcolor[x][y] = mapcolor[x-1][y];
                        }
                        
                    }
                }
                
                DrawMapLine(i); // 绘制地图
            }
            
        }
    
    }
    else
    {
        // 自然下落
        if(timer_cnt % 5 == 0)
        {
            last_block = cur_block;
            DrawClearBlocks(3,3,last_block.block_coord.r,last_block.block_coord.c,last_block.block_array,last_block.block_color);
            cur_block.block_coord.r += 1;
            DrawBlocks(3,3,cur_block.block_coord.r,cur_block.block_coord.c,cur_block.block_array,cur_block.block_color);
            
        }

    }



}




// 游戏结束
void GameEnd(void)
{

}
